# syntax=docker/dockerfile:1

# ---- Stage 1: install dependencies (incl. dev deps, needed to build) ----
FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci

# ---- Stage 2: build the Next.js app ----
FROM node:22-alpine AS builder
WORKDIR /app
# openssl + libc6-compat are required by the Prisma query engine on Alpine (musl).
RUN apk add --no-cache openssl libc6-compat
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# `npm run build` runs `prisma generate && next build`.
RUN npm run build

# ---- Stage 3: lean-ish runtime image ----
FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME=0.0.0.0
RUN apk add --no-cache openssl libc6-compat

# node_modules carries the generated Prisma client + the CLI/tsx used at startup.
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/next.config.ts ./next.config.ts
COPY --from=builder /app/tsconfig.json ./tsconfig.json
COPY --from=builder /app/prisma ./prisma
COPY docker-entrypoint.sh ./docker-entrypoint.sh
RUN chmod +x docker-entrypoint.sh

EXPOSE 3000
# The entrypoint runs `prisma db push` (+ optional seed), then starts the app.
ENTRYPOINT ["./docker-entrypoint.sh"]
